package com.ujs.eduservice.controller.admin;


import com.ujs.commonutils.constant.R;
import com.ujs.eduservice.entity.EduVideo;
import com.ujs.eduservice.service.EduVideoService;
import com.ujs.servicebase.exception.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2023-08-07
 */
@RestController
@RequestMapping("/eduservice/video")
@Api(tags = "小节管理api")
public class EduVideoController {
    @Autowired
    private EduVideoService eduVideoService;

    @ApiOperation("添加小节")
    @PostMapping("/addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo) {
        eduVideoService.save(eduVideo);
        return R.ok();
    }

    /**
     * 删除小节的同时，也需要删除里面的视频
     * @param videoId 小节id
     * @return success
     */
    @ApiOperation("删除小节")
    @DeleteMapping("/deleteVideo/{videoId}")
    public R deleteVideo(@PathVariable("videoId") String videoId) {
        try {
            eduVideoService.removeVideoAndAlyVideo(videoId);
            return R.ok();
        } catch (GuliException e) {
            return R.error().message(e.getMsg());
        }
    }

    @ApiOperation("修改小节")
    @PostMapping("/updateVideo")
    public R updateVideo(@RequestBody EduVideo eduVideo) {
        eduVideoService.updateById(eduVideo);
        return R.ok();
    }

    @ApiOperation("根据id获取小节信息")
    @GetMapping("/getVideo/{videoId}")
    public R getVideo(@PathVariable("videoId") String videoId) {
        EduVideo video = eduVideoService.getById(videoId);
        return R.ok().data("video", video);
    }
}

